package com.example.md.mddemo.controller;

import com.example.md.mddemo.exception.UserException;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * 统一处理一组异常例子,指定处理@Controller注解
 * 要注意@Controller注解包含了RestController注解，所以该类的优先级应该比@RestController的低一些
 * @Date 2018/9/14 16:03
 */
@RestControllerAdvice(annotations = Controller.class)
@Order(-9526)//越小优先级越高
public class UserControllerAdvice {

    /**
     * ControllerAdvice中的ExceptionHandler会对ControllerAdvice作用域中的Controller生效
     * @param e
     * @return
     */
    // 不指定待处理的异常类型，所有UserException的子类型都会被处理
    @ExceptionHandler
    public ModelAndView handleUserException(UserException e){
        ModelAndView mv =  new ModelAndView("error");
        mv.addObject("error", e);
        return mv;
    }
}
